//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: The Resize-Bitmap command.
//
// Creation Date: Jan 15, 2007
//
//---------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Management.Automation;

namespace Pscx.Commands.Drawing
{
    public enum BitmapFormat
    {
        Bmp, Gif, Jpeg, Png, Tiff
    }

    [Cmdlet(VerbsData.Export, PscxNouns.Bitmap)]
    [Description("Exports bitmap objects to various formats.")]
    public class ExportBitmapCommand : BitmapCommandBase
    {
        private const int DefaultQuality = 80;
        private const string JpegQualityPreference = "PscxJpegQualityPreference";

        private string _path;
        private int? _quality;
        private BitmapFormat _format = BitmapFormat.Jpeg;

        private EncoderParameters _params;

        [ValidateNotNullOrEmpty]
        [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)]
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        [DefaultValue("Jpeg")]
        [Parameter(Position = 2, ValueFromPipelineByPropertyName = true)]
        public BitmapFormat Format
        {
            get { return _format; }
            set { _format = value; }
        }

        [DefaultValue(DefaultQuality)]
        [Parameter(Position = 3, ValueFromPipelineByPropertyName = true)]
        [PreferenceVariable(JpegQualityPreference, DefaultQuality)]
        public int? Quality
        {
            get { return _quality; }
            set { _quality = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();

            if (_format == BitmapFormat.Jpeg)
            {
                _params = new EncoderParameters(1);
                _params.Param[0] = new EncoderParameter(Encoder.Quality, Quality.Value);
            }
        }

        protected override void ProcessBitmap(Bitmap bitmap)
        {
            string path = GetUnresolvedProviderPathFromPSPath(_path);
            
            string extension = null;
            ImageFormat format = null;
            ImageCodecInfo encoder = null;

            switch (_format)
            {
                case BitmapFormat.Jpeg:
                    extension = "jpeg";
                    encoder = _jpegEncoder;
                    break;

                case BitmapFormat.Bmp:
                    extension = "bmp";
                    format = ImageFormat.Bmp;
                    break;

                case BitmapFormat.Gif:
                    extension = "gif";
                    format = ImageFormat.Gif;
                    break;

                case BitmapFormat.Png:
                    extension = "png";
                    format = ImageFormat.Png;
                    break;

                case BitmapFormat.Tiff:
                    extension = "tif";
                    format = ImageFormat.Tiff;
                    break;
            }

            path = System.IO.Path.ChangeExtension(path, extension);

            FileHandler.ProcessWrite(path, delegate(Stream stream)
            {
                if(format != null)
                {
                    bitmap.Save(stream, format);
                }
                else if(encoder != null)
                {
                    bitmap.Save(stream, encoder, _params);
                }
            });
        }

        static ExportBitmapCommand()
        {
            foreach (ImageCodecInfo ici in ImageCodecInfo.GetImageEncoders())
            {
                if (ici.MimeType == "image/jpeg")
                    _jpegEncoder = ici;
            }
        }

        private static readonly ImageCodecInfo _jpegEncoder;
    }
}
